package com.yanqu.road.dao.impl.activity.ghost;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ghost.GhostUnionNews;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostUnionNewsDaoImpl extends TempDataObjectDao<GhostUnionNews> {

    public static String getGhostUnionNewsMapSql = "select * from t_c_activity_ghost_union_news where activityId=? and batchNo=?";

    public Map<String, Map<Integer, List<GhostUnionNews>>> getGhostUnionNewsMap(int activityId, int batchNo) {
        Map<String, Map<Integer, List<GhostUnionNews>>> newsMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostUnionNewsMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, batchNo);
            rs = call.executeQuery();
            while (rs.next()) {
                GhostUnionNews ghostUnionNews = parse(rs);

                if (!newsMap.containsKey(ghostUnionNews.getUnionUid())) {
                    newsMap.put(ghostUnionNews.getUnionUid(), new ConcurrentHashMap<>());
                    newsMap.get(ghostUnionNews.getUnionUid()).put(1, new ArrayList<>());
                    newsMap.get(ghostUnionNews.getUnionUid()).put(2, new ArrayList<>());
                }

                newsMap.get(ghostUnionNews.getUnionUid()).get(ghostUnionNews.getType()).add(ghostUnionNews);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return newsMap;
    }

    public static String getGhostUnionNewsMaxIdSql = "select max(id) as maxId from t_c_activity_ghost_union_news where activityId=?";

    public Integer getGhostUnionNewsMaxId(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostUnionNewsMaxIdSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGhostUnionNewsSql = "update t_c_activity_ghost_union_news set bossType=?,bossId=?,userId=? where activityId=? and batchNo=? and unionUid=? and id=?";

    @Override
    protected boolean updateData(GhostUnionNews ghostUnionNews) {
        ghostUnionNews.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getBossType()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getBossId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUnionNews.getUserId()));

        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getBatchNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, ghostUnionNews.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getId()));
        if (getDb().executeNoneQuery(updateGhostUnionNewsSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGhostUnionNewsSql = "insert into t_c_activity_ghost_union_news(activityId,batchNo,unionUid,id,type,bossType,bossId,userId) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GhostUnionNews ghostUnionNews) {
        ghostUnionNews.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getBatchNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, ghostUnionNews.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getBossType()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUnionNews.getBossId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUnionNews.getUserId()));
        if (getDb().executeNoneQuery(insertGhostUnionNewsSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GhostUnionNews parseData(ResultSet rs) throws SQLException {
        GhostUnionNews ghostUnionNews = new GhostUnionNews();
        ghostUnionNews.setActivityId(rs.getInt("activityId"));
        ghostUnionNews.setBatchNo(rs.getInt("batchNo"));
        ghostUnionNews.setUnionUid(rs.getString("unionUid"));
        ghostUnionNews.setId(rs.getInt("id"));
        ghostUnionNews.setType(rs.getInt("type"));
        ghostUnionNews.setBossType(rs.getInt("bossType"));
        ghostUnionNews.setBossId(rs.getInt("bossId"));
        ghostUnionNews.setUserId(rs.getLong("userId"));
        return ghostUnionNews;
    }

}
